
package com.yuke.cloud.service.opc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuke.cloud.common.util.PublicUtil;
import com.yuke.cloud.service.api.opc.dto.gaode.GaodeLocation;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.HashMap;


/**
 * The class Gao de util.
 *
 * @author
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GaoDeUtil {

	private static final String API_KEY = "f8bdce6f882a98635bb0b7b897331327";

	/**
	 * Gets city by ip addr.
	 *
	 * @param ipAddr the ip addr
	 *
	 * @return the city by ip addr
	 */
	public static GaodeLocation getCityByIpAddr(String ipAddr) {
		// http://lbs.amap.com/api/webservice/guide/api/ipconfig/
		log.info("getCityByIpAddr - 根据IP定位. ipAddr={}", ipAddr);
		GaodeLocation location = null;
		String urlAddressIp = "http://restapi.amap.com/v3/ip?key="+API_KEY+"&ip=%s";
		String url = String.format(urlAddressIp, ipAddr);
		try {
			String str = HttpClientUtil.get(HttpConfig.custom().url(url));
			// mod by wg 20181224 如果IP地址是局域网地址时，解析可能会有问题，进行修改
			JSONObject jsonObject = JSON.parseObject(str);
//			location = new ObjectMapper().readValue(str, GaodeLocation.class);
			location = new GaodeLocation();
			if (!jsonObject.getString("province").isEmpty()) {
				location.setProvince(jsonObject.getString("province"));
			}
			if (!jsonObject.getString("city").isEmpty()) {
				location.setCity(jsonObject.getString("city"));
			}

			if (!jsonObject.getString("adcode").isEmpty()) {
				location.setAdcode(jsonObject.getString("adcode"));
			}

			if (!jsonObject.getString("rectangle").isEmpty()) {
				location.setRectangle(jsonObject.getString("rectangle"));
			}

		} catch (Exception e) {
			log.error("getCityByIpAddr={}", e.getMessage(), e);
		}
		log.info("getCityByIpAddr - 根据IP定位. ipAddr={}, location={}", ipAddr, location);
		return location;
	}

	/** * 根据地址获取经纬度 *
	 *
	 * @param addr 地址，规则遵循：国家、省份、城市、区县、城镇、乡村、街道、门牌号码、屋邨、大厦
	 * @param city 所在城市，可选
	 * @return
	 */

	public static Map getLonLarByAddress(String addr, String city) {

		// add by wg 20181225 如果地址中有空格，就会出现异常，将地址按照高德处理方法做处理
		if (PublicUtil.isNotEmpty(addr)) {
			addr = StringUtils.replace(addr, " ", "+");
		}
		if (PublicUtil.isNotEmpty(city)) {
			city = StringUtils.replace(city, " ", "+");
		}

		String url = "http://restapi.amap.com/v3/geocode/geo?key="+API_KEY+"&address="+addr+"&city="+city+"&output=JSON";

		Map<String, String> result = new HashMap<>();

		try {
			String jsonResult = HttpClientUtil.get(HttpConfig.custom().url(url));

			// 解析JSON字符串,取到高德经纬度
			 JSONObject jsonObject = JSON.parseObject(jsonResult);
			 JSONArray geocodes = jsonObject.getJSONArray("geocodes");
			 String geocode = JSON.toJSONString(geocodes.get(0));
			 JSONObject geocodeObj = JSON.parseObject(geocode);
			 String lonAndLat = geocodeObj.getString("location");
			 // 组装结果
			 result.put(addr, lonAndLat);

		} catch (Exception e) {
			log.error("getLonLarByAddress={}", e.getMessage(), e);
		}

		return result;
	}

//	public static void main(String[] args) {
//		String addr = "武汉市光谷青年城4栋1410室";
//		Map<String, String> result = getLonLarByAddress(addr, null);
//
//		log.info(result.get(addr));
//	}
}
